﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using SqlSugar;

#nullable enable
namespace AspnetUtil.Web.Models
{
    public class ApiResult
    {
        /// <summary>
        /// 返回编码
        /// </summary>
        public int Code { get; set; } = 200;

        /// <summary>
        /// 提示信息
        /// </summary>
        public string Message { get; set; } = "";

        public static ApiResult<T> NotFound<T>(string message)
        {
            var result = new ApiResult<T>
            {
                Code = (int) HttpStatusCode.NotFound,
                Message = message
            };
            return result;
        }

        public static ApiResult<T> ParamError<T>(string message = "请求参数错误")
        {
            var result = new ApiResult<T>
            {
                Code = (int) HttpStatusCode.NotFound,
                Message = message
            };
            return result;
        }

        public static ApiResult Success()
        {
            return new ApiResult
            {
                Message = "操作成功"
            };
        }

        public static ApiResult<T> Success<T>(T t)
        {
            return new ApiResult<T>
            {
                Message = "操作成功",
                Data = t
            };
        }

        /// <summary>
        /// 返回分页的结果
        /// </summary>
        /// <param name="queryable"> linq </param>
        /// <param name="param"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static ApiResult<PageResult<T>> Page<T>(IQueryable<T> queryable, PageParam param)
        {
            var count = queryable.Count();
            List<T> list = queryable.Skip((param.Page - 1) * param.Size).Take(param.Size).ToList();

            var result = new PageResult<T>
            {
                Total = count,
                List = list
            };
            return Success(result);
        }


        /// <summary>
        /// sqlsugar 分页.
        /// </summary>
        /// <param name="queryable"></param>
        /// <param name="param"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static ApiResult<PageResult<T>> Page<T>(ISugarQueryable<T> queryable, PageParam param)
        {
            int totalCount = 0;
            var list = queryable.ToPageList(param.Page, param.Size, ref totalCount);

            var result = new PageResult<T>
            {
                Total = totalCount,
                List = list
            };
            return Success(result);
        }

        public static ApiResult<PageResult<T>> Page<T>(PageResult<T> result)
        {
            return Success(result);
        }
    }

    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// 响应信息
        /// </summary>
        public T? Data { get; set; }
    }
}